package me.zhengjie.minbearwcs.dao.inbound.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import me.zhengjie.minbearwcs.dao.inbound.inf.InboundOrderDetailDao;
import me.zhengjie.minbearwcs.entity.Mapper.inbound.InboundOrderDetailMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.inbound.InboundOrderDetail;
import me.zhengjie.minbearwcs.entity.PO.wcs.inbound.status.InboundOrderDetailStatus;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class InboundOrderDetailDaoImpl implements InboundOrderDetailDao {

    private final InboundOrderDetailMapper mapper;

    @Override
    public boolean insert(InboundOrderDetail inboundOrderDetail) {
        return mapper.insert(inboundOrderDetail) == 1;
    }

    @Override
    public InboundOrderDetail selectDetailByOrderCodeAndPalletCode(String orderCode, String palletCode, InboundOrderDetailStatus status) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getInboundOrderCode, orderCode)
                .eq(InboundOrderDetail::getPalletCode, palletCode)
                .eq(InboundOrderDetail::getStatus, status)
                .one();
    }

    @Override
    public boolean updateStartPointById(Integer id, String startPoint) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getId, id)
                .set(InboundOrderDetail::getStartPoint, startPoint)
                .update();
    }

    @Override
    public boolean updateStatusById(Integer id, InboundOrderDetailStatus status) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getId, id)
                .set(InboundOrderDetail::getStatus, status)
                .update();
    }

    @Override
    public InboundOrderDetailStatus getStatusById(Integer id) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getId, id)
                .select(InboundOrderDetail::getStatus)
                .one()
                .getStatus();
    }

    @Override
    public boolean updateStoragePointById(Integer id, String storagePoint) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getId, id)
                .set(InboundOrderDetail::getStoragePoint, storagePoint)
                .update();
    }

    @Override
    public boolean updateExchangePointById(Integer id, String exchangePoint) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getId, id)
                .set(InboundOrderDetail::getExchangePoint, exchangePoint)
                .update();
    }


    @Override
    public InboundOrderDetail selectDetailByOrderCodeAndPalletCode(String palletCode, InboundOrderDetailStatus status) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getPalletCode, palletCode)
                .eq(InboundOrderDetail::getStatus, status)
                .one();
    }

    @Override
    public InboundOrderDetail selectById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public Integer selectIdByInboundOrderAndPallet(String inboundOrderCode, String palletCode) {
        InboundOrderDetail inboundOrderDetail = new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getInboundOrderCode, inboundOrderCode)
                .eq(InboundOrderDetail::getPalletCode, palletCode)
                .oneOpt()
                .orElse(null);
        return inboundOrderDetail == null ? null : inboundOrderDetail.getId();

    }

    @Override
    public InboundOrderDetail selectByPalletAndStoragePoint(String pallet, String storagePoint) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getPalletCode, pallet)
                .eq(InboundOrderDetail::getStoragePoint, storagePoint)
                .one();
    }

    @Override
    public List<InboundOrderDetail> selectByInboundOrderCode(String inboundOrderCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(InboundOrderDetail::getInboundOrderCode, inboundOrderCode)
                .list();
    }

    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteById(id) == 1;
    }
}
